<?php

namespace app\admin\model;

use app\api\model\common\CoinModel;
use app\api\model\user\UserModel;
use think\Model;
use traits\model\SoftDelete;

class OrderEarnings extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order_earnings';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];
    

    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function mineUser()
    {
        return $this->belongsTo(UserModel::class,'user_id','id',[],'LEFT')
            ->setEagerlyType(0);
    }
    public function mineCoin()
    {
        return $this->belongsTo(CoinModel::class,'coin_id','id',[],'LEFT')
            ->setEagerlyType(0);
    }

}
